
var express = require("express");
var router = express.Router();
const { getIpService } =  require("../service/ipAddressService");
const { ipv6ToV4 } = require('../utils/tool')
router.get("/", async function (req, res, next) {
  let ip = req.headers['x-forwarded-for'] ||
	req.connection.remoteAddress ||
	req.socket.remoteAddress ||
	req.connection.socket.remoteAddress ||
	'';
  // console.log(ip,'<--ip');
  // 判断访问的ip是不是ipv6,如果是则转换成ipv4,否则直接使用
  // 通过正则判断是否是ipv6
  let ipv6Reg = /:/g;
  let ipv4 = ip;
  if(ipv6Reg.test(ip)){
     ipv4 = ipv6ToV4(ip);
  }
  res.send(await getIpService(ipv4))
});
module.exports = router;